<template>
    <el-card>
        <el-table :data="tableData" style="width: 100%" border>
            <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
            <el-table-column label="名称" show-overflow-tooltip prop="skuName"></el-table-column>
            <el-table-column label="描述" show-overflow-tooltip prop="skuDesc"></el-table-column>
            <el-table-column label="图片">
                <template #="{ row }">
                    <img :src="row.skuDefaultImg" alt="" style="width: 100px;height: 100px;">
                </template>
            </el-table-column>
            <el-table-column label="重量" prop="weight"></el-table-column>
            <el-table-column label="价格" prop="price"></el-table-column>
            <el-table-column label="操作" fixed="right">
                <template #="{ row }">
                    <el-button type="primary" size="small" :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
                        @click="updateSale(row)"></el-button>
                    <el-button type="primary" size="small" icon="Edit" @click="updateSku"></el-button>
                    <el-button type="primary" size="small" icon="InfoFilled" @click="findSku(row)"></el-button>
                    <el-popconfirm :title="`你确定要删除${row.skuName}?`" width="200px" @confirm="removeSku(row.id)">
                        <template #reference>
                            <el-button type="primary" size="small" icon="Delete"></el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="page" v-model:page-size="limit" :page-sizes="[5, 8, 10, 15]"
            layout=" prev, pager, next, jumper,->,sizes,total" :total="total" @size-change="getSKU()"
            @current-change="getSKU" />
        <el-drawer v-model="drawer">
            <template #header>
                <h4>查看商品的详情</h4>
            </template>
            <template #default>
                <el-row style="margin:10px 0px;">
                    <el-col :span="6">名称</el-col>
                    <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                </el-row>
                <el-row style="margin:10px 0px;">
                    <el-col :span="6">描述</el-col>
                    <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
                </el-row>
                <el-row style="margin:10px 0px;">
                    <el-col :span="6">价格</el-col>
                    <el-col :span="18">{{ skuInfo.price }}</el-col>
                </el-row>
                <el-row style="margin:10px 0px;">
                    <el-col :span="6">平台属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin:5px;" v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{
                            item.valueName }}</el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin:10px 0px;">
                    <el-col :span="6">销售属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin:5px;" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id">{{
                            item.saleAttrValueName }}</el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin:10px 0px;">
                    <el-col :span="6">商品图片</el-col>
                    <el-col :span="18">
                        <el-carousel :interval="4000" type="card" height="200px">
                            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                <img :src="item.imgUrl" alt="" style="width:100%;height: 100%;">
                            </el-carousel-item>
                        </el-carousel>
                    </el-col>
                </el-row>
            </template>

        </el-drawer>
    </el-card>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { skuList, onSale, cancelSale, getSkuInfo, deleteSku } from '@/api/product/sku';
import type { GetSKU, SKUValue, SKUData } from '@/api/product/sku/type'
import { ElMessage } from 'element-plus';
// 分页器
const page = ref<number>(1)
const limit = ref<number>(3)
const total = ref<number>()
// 抽屉框
const drawer = ref<boolean>(false)
// sku表格数据
const tableData = ref<SKUValue[]>([])
let skuInfo = reactive<any>({})
// 获取sku
onMounted(() => {
    getSKU()
})
// 获取sku
const getSKU = async (num: number = 1) => {
    page.value = num
    const data: GetSKU = await skuList(page.value, limit.value)
    tableData.value = data.data.records
    total.value = data.data.total

}
// 上架下架
const updateSale = async (row: SKUValue) => {
    if (row.isSale == 0) {
        const data = await onSale(row.id)
        if (data.code !== 200) return
        ElMessage.success('上架成功！')
        getSKU(page.value)
    } else {
        const data = await cancelSale(row.id)
        if (data.code !== 200) return
        ElMessage.success('下架成功！')
        getSKU(page.value)
    }

}
const updateSku = () => {
    ElMessage.success('程序员正在开发中....')
}
// 弹出抽屉详情
const findSku = async (row: SKUValue) => {
    const data: SKUData = await getSkuInfo(row.id)
    skuInfo = data.data
    drawer.value = true
}
// 删除以后sku
const removeSku = async (id: number) => {
    const data = await deleteSku(id)
    console.log(data)
    if (data.code == 200) {
        ElMessage.success("删除成功")
        getSKU(tableData.value.length > 1 ? page.value : page.value - 1)
    } else {
        ElMessage.success("删除失败")
    }

}


</script>

<style scoped ></style>